<template>
  <div>
    <!-- 表单 -->
    <el-form size="small" :model="form" :inline="true">
      <el-form-item label="学生姓名" style="font-weight:bold;">
        <el-input v-model="form.userName" placeholder="请输入学生姓名" style="width:180px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="学 号" style="font-weight:bold;">
        <el-input v-model="form.userNum" placeholder="请输入学号" style="width:180px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="活动/课程名称" style="font-weight:bold;">
        <el-input v-model="form.className" placeholder="请输入活动/课程名称" style="width:180px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="活动/课程状态" style="font-weight:bold;">
        <el-select v-model="form.state" placeholder="请选择活动/课程状态" style="width:180px;" clearable>
          <el-option label="未开始" value="0"></el-option>
          <el-option label="进行中" value="1"></el-option>
          <el-option label="已结束" value="2"></el-option>
          <el-option label="已暂停" value="3"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="发起人" style="font-weight:bold;">
        <el-input v-model="form.userNum" placeholder="请输入" style="width:180px;" clearable></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="班 级" style="font-weight:bold;">
        <el-select v-model="form.status" placeholder="请选择班级" style="width:180px;" clearable>
          <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option>
        </el-select>
      </el-form-item> -->

    </el-form>
    <!-- 表单 -->
    <el-button size="small" class="search_botton" type="primary" icon="el-icon-search" @click="getUserList">查询</el-button>
    <el-button size="small" class="reset_botton" icon="el-icon-refresh" @click="reset">重置</el-button>

    <el-button plain class="addBtn" size="mini" type="success" icon="el-icon-folder-add"
      @click="memberManage = true">新增学生</el-button>

    <el-divider></el-divider>

    <!-- 表格 -->
    <el-table size="medium" :data="tableData" border style="width: 100%">
      <el-table-column prop="userName" label="学生姓名" align="center"></el-table-column>
      <el-table-column prop="userNum" label="学号" align="center"></el-table-column>
      <el-table-column prop="className" label="活动/课程名称" align="center"></el-table-column>
      <el-table-column prop="startDate" label="活动/课程开始时间" align="center"></el-table-column>
      <el-table-column prop="endDate" label="活动/课程结束时间" align="center"></el-table-column>
      <!-- <el-table-column prop="" label="班级" align="center"></el-table-column> -->
      <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
      <el-table-column prop="type" label="活动/课程状态" align="center">
        <template slot-scope="scope">
          <span v-if="(scope.row.type == 0)">未开始</span>
          <span v-if="(scope.row.type == 1)">进行中</span>
          <span v-if="(scope.row.type == 2)">已结束</span>
          <span v-if="(scope.row.type == 3)">已暂停</span>
        </template>
      </el-table-column>
      <el-table-column prop="" label="成绩" align="center" width="90">
        <template slot-scope="">
          <el-button size="mini" type="success" plain>查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="primary" plain>修改</el-button> -->
          <el-button size="mini" type="danger" plain @click="removeUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>

    <el-dialog title="新增学生" :visible.sync="memberManage" width="50%" center>
      <el-form label-position="right" size="small" :model="formNewMember" label-width="200px">
        <el-form-item label="学生姓名" style="font-weight:bold;">
          <el-input v-model="formNewMember.name" placeholder="请输入学生姓名" style="width:400px;" clearable></el-input>
        </el-form-item>
        <el-form-item label="学号" style="font-weight:bold;">
          <el-input v-model="formNewMember.num" placeholder="请输入学号" style="width:400px;" clearable></el-input>
        </el-form-item>
        <!-- <el-form-item label="班级" style="font-weight:bold;">
          <el-input v-model="formNewMember.userNum" placeholder="请输入班级" style="width:400px;" clearable></el-input>
        </el-form-item> -->
        <el-form-item label="联系方式" style="font-weight:bold;">
          <el-input v-model="formNewMember.phone" placeholder="请输入手机号" style="width:400px;" clearable></el-input>
        </el-form-item>
        <el-form-item label="活动/课程" style="font-weight:bold;">
          <el-select v-model="formNewMember.id" placeholder="请选择活动/课程" style="width:400px;" clearable>
            <el-option v-for="item in courses" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="addMember">确定新增</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, getCourses, removeUser } from '@/api/PalyEducation/studentManagement/index.js'
import { addMember } from '@/api/PalyEducation/courseManagement/index.js'
export default {
  data() {
    return {
      memberManage: false,
      formNewMember: {
        id: '',
        name: "",
        num: "",
        phone: ""
      },
      form: {
        userName: "",
        userNum: "",
        className: "",
        state: "",
        current: 0,
        pageSize: 10,
      },
      courses: [],
      tableData: [],
      total: 0
    }
  },
  created() {
    this.getUserList()
    this.getCourses()
  },
  methods: {
    reset() {
      this.form = {
        userName: "",
        userNum: "",
        className: "",
        state: "",
        current: 0,
        pageSize: 10,
      }
      this.getUserList()
    },
    //新增活动成员
    addMember() {
      addMember(this.formNewMember).then(res => {
        if (res.data.success == true) {
          this.memberManage = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.getUserList()
            }
          })
        } else {
          this.$message.info(res.data.message)
        }
      })
    },
    //  删除成员
    removeUser(e) {
      this.$confirm('此操作将删除该学生, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeUser({ id: e }).then(res => {
          if (res.data.success == true) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.getUserList()
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCurrentChange(e) {
      //当前页
      this.form.current = (e - 1) * 10
      this.getUserList()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getUserList()
    },
    getUserList() {
      getUserList(this.form).then(res => {
        if (res.data.success == true) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
    getCourses() {
      getCourses().then(res => {
        if (res.data.success == true) {
          this.courses = res.data.data
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.pagination {
  float: right;
  margin-top: 10px;
}
</style>